//
//  HealthViewController.m
//  FNMenu
//
//  Created by 航汇聚科技 on 2018/9/25.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "HealthViewController.h"
#import "NSDictionary+Safety.h"
#import "NSArray+Safety.h"
#import "HealthTableViewCell.h"
#import "HealthKitStore.h"
#import "NetManager.h"
@interface HealthViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *stepCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *calorieLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSDictionary *threeMealsADay;
@property (nonatomic, strong) HealthKitStore *healthStore;
@end

@implementation HealthViewController

#pragma mark - lifeCycle
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.tableFooterView = [UIView new];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.healthStore readStepCount];
    __weak typeof(self) ws = self;
    _healthStore.allStepCount = ^(int stepCount) {
        dispatch_async(dispatch_get_main_queue(), ^{
            ws.stepCountLabel.text = [NSString stringWithFormat:@"%d 步",stepCount];
            ws.calorieLabel.text = [NSString stringWithFormat:@"%.f 卡", stepCount * 0.04];
        });
    };
    
    [[NetManager defaultNetManager] fetchNetDataWithURLStr:@"home/users/threeMealsADay" params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.threeMealsADay = [responseObject safeObjectForKey:@"show_data"];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [[self.threeMealsADay safeObjectForKey:@"breakfast"] count];
    }
    if (section == 1) {
        return [[self.threeMealsADay safeObjectForKey:@"lunch"] count];
    }
    if (section == 2) {
        return [[self.threeMealsADay safeObjectForKey:@"dinner"] count];
    }
    return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HealthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"healthCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *meals = @[];
    if (indexPath.section == 0) {
        meals = [self.threeMealsADay safeObjectForKey:@"breakfast"];
    }
    if (indexPath.section == 1) {
        meals = [self.threeMealsADay safeObjectForKey:@"lunch"];
    }
    if (indexPath.section == 2) {
        meals = [self.threeMealsADay safeObjectForKey:@"dinner"];
    }
    cell.foodNameLabel.text = [[meals safeObjectAtIndex:indexPath.row] safeObjectForKey:@"name"];
    cell.foodCalarieLabel.text = [NSString stringWithFormat:@"%@ 卡", [[meals safeObjectAtIndex:indexPath.row] safeObjectForKey:@"calarie"]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    if (section == 0) {
        label.text = @"\t早餐";
    }
    if (section == 1) {
        label.text = @"\t午餐";
    }
    if (section == 2) {
        label.text = @"\t晚餐";
    }
    UIFont *font = [UIFont fontWithName:@"AppleSDGothicNeo-Regular" size:20.0];
    label.font = font;
    return label;
}
#pragma mark = event
- (IBAction)sportAction:(UIButton *)sender {
    NSLog(@"运动");
}
#pragma mark - getter
- (HealthKitStore *)healthStore {
    if (!_healthStore) {
        _healthStore = [[HealthKitStore alloc] init];
    }
    return _healthStore;
}
@end
